package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.dao.UserTeamMapper;
import com.coolwen.entity.Challenge;
import com.coolwen.entity.Team;
import com.coolwen.entity.User;
import com.coolwen.service.ChallengeService;
import com.coolwen.service.TeamService;
import com.coolwen.service.UserTeamService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * Created by Zero on 2016/12/5.
 */
@Controller
@RequestMapping(value = {"/team/team"})
public class TeamController {


    @Autowired
    TeamService teamService;


    @Autowired
    UserTeamMapper userTeamMapper;

    @Autowired
    UserTeamService userTeamService;


    @Autowired
    ChallengeService challengeService;

    private static final Logger LOGGER = Logger.getLogger(TeamController.class);



    /**
     * 通过Id显示球队信息
     * @param model
     * @return
     */

    @RequestMapping(value = "/list")
    public String listALL(Model model,HttpSession session) {

        User user = (User) session.getAttribute("loginUser");

        Integer teamId=userTeamMapper.selectUserTeamByUserID(user.getUserId()).getTeamId(); //获取球队Id

        model.addAttribute("team", teamService.findTeamById(teamId));   //获取球队信息

        model.addAttribute("countMember",teamService.CountMemberByTeamId(teamId));    //获取球队人数

        model.addAttribute("countChallenge",teamService.CountChallengeByTeamId(teamId)); //获取约战次数

        //主场胜负平
        model.addAttribute("win",teamService.CountChallengeResultByTeamId("1", teamId));  //获取胜场

        model.addAttribute("fail",teamService.CountChallengeResultByTeamId("2", teamId));  //获取负场

        model.addAttribute("flat",teamService.CountChallengeResultByTeamId("3", teamId));  //获取平场


        return "team/team";
    }





    /**
     * 添加一个球队信息
     * @param model
     * @return
     */


//新用户登陆后访问这个页面然后编辑球队
    @RequestMapping(value="/addTeam",method= RequestMethod.GET)
    public String add(Model model) {

        model.addAttribute(new Team());
        return "team/addTeam";
    }

//添加球队成功后进入首页
    @RequestMapping(value="/addTeam",method=RequestMethod.POST)
    public String add(@Validated Team team,HttpSession session){
        User user = (User) session.getAttribute("loginUser");

        teamService.addTeam(team, user.getUserId());

        return "redirect:/";
    }





    /**
     * 修改球队信息
     * @param
     * @return
     */



    //修改球队过后跳到球员概况页面
    @RequestMapping(value="/update",method=RequestMethod.POST)
    public String update(@Validated Team team,HttpSession session,Model model){

        User user = (User) session.getAttribute("loginUser");

        Integer teamId=userTeamMapper.selectUserTeamByUserID(user.getUserId()).getTeamId();

        model.addAttribute("team", teamService.updateTeam(team, teamId));

        return "redirect:/team/team/list";
    }


    /**点击约战进入约战界面
     * 并添加一个约战信息
     * @param model
     * @return
     */


//新用户登陆后访问这个页面然后编辑球队
    @RequestMapping(value="/addUserTeam/{teamId}",method= RequestMethod.GET)
    public String addChallenge(@PathVariable int teamId,Model model) {

        model.addAttribute("team", teamService.findTeamById(teamId));

        model.addAttribute(new Challenge());

        return "challenge/challengeInvitation";
    }

    //跳转到约战已发送界面界面
    //传入约战信息，主场Id,客场Id
    @RequestMapping(value="/addUserTeam/{teamId}",method=RequestMethod.POST)
    public String addChallenge(@Validated Challenge challenge,@PathVariable int teamId,HttpSession session){


        User user = (User) session.getAttribute("loginUser");

        Integer sendId=userTeamService.findUserTeamByUserID(user.getUserId()).getTeamId();

        String sendName=teamService.findTeamById(sendId).getTeamName(); //获取主场名字

        String receiveName=teamService.findTeamById(teamId).getTeamName(); //获取客场名字

        challengeService.addChallenge(challenge, sendId, teamId, sendName, receiveName); //将约战信息和主客场Id，主客场名字插入数据库


        return "redirect:/challenge/Challenge/send";
    }




    @RequestMapping(value = "/map")
    public String map(){

        return "team/map";
    }





    @RequestMapping(value="/select",method= RequestMethod.GET)
    public String selectTeam() {

        return "challenge/challengesingleteam";
    }



    @RequestMapping(value="/select",method=RequestMethod.POST)
    public String selectTeam(@Validated String teamName,Model model){


        LOGGER.info("-------->>>>>>>>>>>" + JSON.toJSON(teamName));

        model.addAttribute("singleTeam", teamService.findTeamByTeamName(teamName));

        return "challenge/challengesingleteam";
    }



}



